//
//  MStatusPictureView.swift
//  iOSweibo
//
//  Created by lzm on 16/5/20.
//  Copyright © 2016年 lzm. All rights reserved.
//

import UIKit
import SnapKit

/// 最大列数
private let MaxColumn = 3

/// imageView之间的间距
private let ImageViewMargin: CGFloat = 4

// 计算一个图片的宽度和高度
// (屏幕的宽度 - 2 * CellMargin - (MaxColumn - 1) * ImageViewMargin) / MaxColumn
private let MStatusPictureViewImageViewWH = (UIScreen.mainScreen().bounds.width - 2 * cellMargin - (CGFloat(MaxColumn) - 1) * ImageViewMargin) / CGFloat(MaxColumn)

class MStatusPictureView: UIView {

    //显示图片
    var pictureURLs : [NSURL]?{
        didSet{
            let size = layoutImages(pictureURLs)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
        }
    }
    //布局九张图
    private func layoutImages(pictureURLS:[NSURL]?)->CGSize
    {
        let count = pictureURLs?.count ?? 0
        
        var numofCols = 3
        if count == 1 {
            numofCols = 1
        }else if count == 2 || count == 4
        {
            numofCols = 2
        }
        
        var index = 0
        //遍历图片数组
        for imageView in imageViews{
            if index >= count{
                imageView.hidden = true
                
            }
            else{
                imageView.hidden = false
                //列
                let cols = index % numofCols
                //行
                let rows = index / numofCols
                
                let x = CGFloat(cols) * (MStatusPictureViewImageViewWH + ImageViewMargin)
                let y = CGFloat(rows) * (MStatusPictureViewImageViewWH + ImageViewMargin)
                
                
                imageView.frame = CGRect(x: x, y: y, width: MStatusPictureViewImageViewWH, height: MStatusPictureViewImageViewWH)
                
                //设置图片
                let url = pictureURLs?[index]
                imageView.sd_setImageWithURL(url)
            }
           
            index++
        }
        //计算配图的大小
        if count == 0{
            return CGSizeZero
        }
        //总行数 = (count + 列数 - 1) / 列数
        let numOfRow = (count + numofCols-1) / numofCols
        // width = 列数 * imageView.width + (列数 - 1) * 间距
        let width = CGFloat(numofCols) * MStatusPictureViewImageViewWH + (CGFloat(numofCols) - 1) * ImageViewMargin
        // height = 行数 * imageView.height + (行数 - 1) * 间距
        let height = CGFloat(numOfRow) * MStatusPictureViewImageViewWH + (CGFloat(numOfRow) - 1) * ImageViewMargin
        return CGSize(width: width, height: height)
    }
    
    //定义图片空数组
    private var imageViews = [UIImageView]()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    private func prepareUI()
    {
        

        for _ in 0..<9{
            
            let imageView = UIImageView()
            imageView.backgroundColor = UIColor.randomColor()
            addSubview(imageView)
            imageViews.append(imageView)
            
        }
//        addSubview(debugLabel)
    }
    
    private lazy var debugLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.blackColor()
        label.text = "我没有图片"
        label.frame = CGRect(x: 20, y: 20, width: 100, height: 100)
        
        return label
    }()
    
}
